from django.db import models

from apps.user.models import UserModel


# Create your models here.
class InterfaceModel(models.Model):
    class Meta:
        db_table = 'interface'
        verbose_name = '接口信息'
        verbose_name_plural = verbose_name

    METHOD_CHOICES = (('GET', 'get'), ('POST', 'post'), ('PUT', 'put'),
                      ('PATCH', 'patch'), ('DELETE', 'delete'), ('HEAD', 'head'),
                      ('OPTIONS', 'options'))

    user_id = models.ForeignKey(null=False, to=UserModel, on_delete=models.CASCADE, verbose_name='创建者id')
    name = models.CharField(null=False, max_length=32, verbose_name='接口名')
    project_id = models.IntegerField(null=False, verbose_name='项目id')
    method = models.CharField(null=False, max_length=10, choices=METHOD_CHOICES, default=METHOD_CHOICES[0],
                              verbose_name='请求方法')
    url = models.TextField(null=False, verbose_name='请求url')
    data = models.TextField(null=False, default='{"body": "", "params":""}', verbose_name='请求数据')
    header = models.TextField(null=False, default='{}', verbose_name='请求头')
    create_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    timeout = models.IntegerField(null=False, default=10, verbose_name='超时时间')
    response_expected = models.TextField(null=False, default='')

    def __str__(self):
        return self.name


class InterfaceLogModel(models.Model):
    class Meta:
        db_table = 'interface_log'
        verbose_name = '接口测试记录'
        verbose_name_plural = verbose_name

    interface_id = models.ForeignKey(null=False, on_delete=models.CASCADE, to=InterfaceModel, verbose_name='接口id')
    start_time = models.DateTimeField(auto_now_add=True, verbose_name='开始测试时间')
    response_body = models.TextField(null=False, default='', verbose_name='响应体')
    response_header = models.TextField(null=False, default='{}', verbose_name='响应头')
    status_code = models.IntegerField(null=False, verbose_name='响应码')
    remote_address = models.CharField(null=False, max_length=32, verbose_name='服务器地址')
    used = models.IntegerField(null=False, verbose_name='响应时间')
    succeed = models.BooleanField(null=False, verbose_name='是否成功')

    def __str__(self):
        return self.status_code
